package it.low.game.server.client;

import it.low.game.core.element.ActiveElement;
import it.low.game.core.location.MapPoint;
import it.low.game.core.message.ClientEvent;
import it.low.game.core.message.ElementState;
import it.low.game.core.message.PlayerEvent;
import it.low.game.server.ServerEngine;
import it.low.game.server.persistence.ServerStoring;
import it.low.game.server.util.ServerLogManager;

import java.util.Collection;
import java.util.Set;

public class ServerClientInterfaceImpl implements ServerClientInterface {

	public int init(long clientId){
		ServerLogManager.logMsg("call ServerClientInterfaceImpl.init("+clientId+")");
		return 0;
	}
	
	public void close(long clientId){
		ServerLogManager.logMsg("call ServerClientInterfaceImpl.close("+clientId+")");
	}
	
	// RECEIVE EVENTS FROM CLIENT
	
	public void receiveClientEvent(ClientEvent clientEvent){
		ServerLogManager.logMsg("call ServerClientInterfaceImpl.receiveClientEvent("+clientEvent.getActionType()+")");
		//ServerEngine.processEvent(clientEvent);
	}
	
	public void receivePlayerEvent(PlayerEvent playerEvent){
		ServerLogManager.logMsg("call ServerClientInterfaceImpl.receivePlayerEvent("+playerEvent.getActionType()+")");
		ServerEngine.processEvent(playerEvent);
	}
	
	// SEND WORLD STATE TO THE CLIENTS
	
	public void sendWorldState(long elapsedTime){
		ServerEngine.update(elapsedTime);
	}
	
	public ElementState sendPlayerState(long playerId){
		return ServerEngine.getPlayerState(playerId);
	}
	
	public Set<ElementState> sendElementsState(long playerId){
		return ServerEngine.getElementStates(playerId);
	}	
	
	// TO START THE GAME METHODS
	
	public ActiveElement loadPlayer(long clientId){
		ServerLogManager.logMsg("call ServerClientInterfaceImpl.loadPlayer("+clientId+")");
		ActiveElement activeElementPlayer = ServerStoring.getElementPlayer(clientId);
		if(activeElementPlayer.isInit()){
			ServerLogManager.logMsg("call ServerClientInterfaceImpl.loadPlayer() inizializated");
			ServerEngine.setPlayer(activeElementPlayer);	
		}else{
			ServerLogManager.logMsg("call ServerClientInterfaceImpl.loadPlayer() not inizializated");
		}
		return activeElementPlayer;
	}
	
	public String[][] loadMap(){
		ServerLogManager.logMsg("call ServerClientInterfaceImpl.loadMap()");
		String[][] mapDescription = ServerStoring.getMapDecription();
		return mapDescription;
	}	
		
	public Collection<ActiveElement> loadBots(){
		ServerLogManager.logMsg("call ServerClientInterfaceImpl.loadBots()");
		Collection<ActiveElement> botsDescription = ServerStoring.getBotDecription().values();
		return botsDescription;
	}
	
	public ActiveElement initPlayer(long playerId, int size, MapPoint mapPosition){
		ServerLogManager.logMsg("call ServerClientInterfaceImpl.initPlayer("+playerId+","+size+","+mapPosition+")");
		ActiveElement activeElementPlayer = ServerStoring.getElementPlayer(playerId);
		activeElementPlayer.init(size, mapPosition);
		ServerEngine.setPlayer(activeElementPlayer);
		return activeElementPlayer;
	}	
	
	public ActiveElement initBot(long botId, int size){
		ServerLogManager.logMsg("call ServerClientInterfaceImpl.initBot("+botId+","+size+")");
		ActiveElement activeElementBot = ServerStoring.getElementBot(botId);
		activeElementBot.init(size,ServerEngine.getRandomPoint());
		ServerEngine.setBot(activeElementBot);
		return activeElementBot;
	}	
	
}
